package com.ggh.jinjilive.ui.mall.shop;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentPendingBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.shop.MineShopOrder;
import com.ggh.jinjilive.mobile.mall.shop.MineShopOrderTask;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mall.shop.adapter.PendingAdapter;
import com.ggh.jinjilive.ui.mall.shop.adapter.PendingTaskAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class PendingFragment extends BaseFragment<FragmentPendingBinding> implements View.OnClickListener, OnRefreshListener, OnLoadMoreListener {
    private MineShopViewModel mineShopViewModel;
    private int status = 2;
    //适配器
    private PendingAdapter pendingAdapter;
    //任务适配器
    private PendingTaskAdapter pendingTaskAdapter;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        mineShopViewModel = new ViewModelProvider(this,viewModelFactory).get(MineShopViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_pending;
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);

    }



    @Override
    public void initView() {
        super.initView();
        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        //订单适配器
        pendingAdapter = new PendingAdapter();
        binding.rvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        //任务适配器
        pendingTaskAdapter = new PendingTaskAdapter();
        pendingTaskAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                if(position == 99999999){
                    ARouter.getInstance().build(ARouterUtils.AddTaskActivity)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .navigation();
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        mineShopViewModel.isRefresh = true;
        mineShopViewModel.orderList(status);

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //新增订单
        binding.imgNewCentre.setOnClickListener(this);
        //待收订单
        binding.imgAcceptNot.setOnClickListener(this);
        //订单记录
        binding.imgRecordNot.setOnClickListener(this);
        //付款记录
        binding.imgRefundNot.setOnClickListener(this);
        //认为订单
        binding.imgTaskNot.setOnClickListener(this);

        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
        //获取订单
        mineShopViewModel.MineShopOrderList.observe(this, new Observer<List<MineShopOrder>>() {
            @Override
            public void onChanged(List<MineShopOrder> mineShopOrders) {
                binding.refreshLayout.finishRefresh();
                if(mineShopViewModel.isRefresh){
                    if(mineShopOrders.size() > 0){
                        loadService.showSuccess();
                        pendingAdapter.setAdd(mineShopOrders);
                        binding.rvGoods.setAdapter(pendingAdapter);
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(mineShopOrders.size() > 0){
                        pendingAdapter.ADD(mineShopOrders);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
        //任务列表
        mineShopViewModel.MineShopOrderTaskList.observe(this, new Observer<List<MineShopOrderTask>>() {
            @Override
            public void onChanged(List<MineShopOrderTask> mineShopOrderTasks) {
                binding.refreshLayout.finishRefresh();
                if(mineShopViewModel.isRefresh){
                    loadService.showSuccess();
                    if (mineShopOrderTasks.size() <= 0) {
                        mineShopOrderTasks.add(0, null);
                    }
                    pendingTaskAdapter.setAdd(mineShopOrderTasks);
                    binding.rvGoods.setAdapter(pendingTaskAdapter);
                }else {
                    if(mineShopOrderTasks.size() > 0){
                        pendingTaskAdapter.ADD(mineShopOrderTasks);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_new_centre://新订单
                //默认订单
                defaultOrder();
                //新增订单
                binding.imgNewCentre.setImageResource(R.mipmap.icon_new_centre);
                status = 2;
                mineShopViewModel.isRefresh = true;
                mineShopViewModel.orderList(status);
                break;
            case R.id.img_accept_not://待收订单
                //默认订单
                defaultOrder();
                //待收订单
                binding.imgAcceptNot.setImageResource(R.mipmap.icon_accept_centre);
                status = 3;
                mineShopViewModel.isRefresh = true;
                mineShopViewModel.orderList(status);
                break;
            case R.id.img_record_not://订单记录
                //默认订单
                defaultOrder();
                //订单记录
                binding.imgRecordNot.setImageResource(R.mipmap.icon_record_centre);
                status = 4;
                mineShopViewModel.isRefresh = true;
                mineShopViewModel.orderList(status);
                break;
            case R.id.img_refund_not://付款记录
                //默认订单
                defaultOrder();
                //付款记录
                binding.imgRefundNot.setImageResource(R.mipmap.icon_refund_centre);
                status = 11;
                mineShopViewModel.isRefresh = true;
                mineShopViewModel.orderList(status);
                break;
            case R.id.img_task_not://认为订单
                //默认订单
                defaultOrder();

                //认为订单
                binding.imgTaskNot.setImageResource(R.mipmap.icon_task_centre);
                status = 999999;
                onRefresh(binding.refreshLayout);
                break;
        }
    }
    //默认订单
    private void defaultOrder(){
        //新增订单
        binding.imgNewCentre.setImageResource(R.mipmap.icon_new_not);
        //待收订单
        binding.imgAcceptNot.setImageResource(R.mipmap.icon_accept_not);
        //订单记录
        binding.imgRecordNot.setImageResource(R.mipmap.icon_record_not);
        //付款记录
        binding.imgRefundNot.setImageResource(R.mipmap.icon_refund_not);
        //认为订单
        binding.imgTaskNot.setImageResource(R.mipmap.icon_task_not);
    }



    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        mineShopViewModel.isRefresh = true;
        if(status == 999999){
            mineShopViewModel.taskList();
        }else {
            mineShopViewModel.orderList(status);
        }

    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        mineShopViewModel.isRefresh = false;
        if(status == 999999){
            mineShopViewModel.taskList();
        }else {
            mineShopViewModel.orderList(status);
        }
    }

}
